import Vue from "vue";
import Vuex from "vuex"

Vue.use(Vuex)

const store=new Vuex.Store({
  strict:true,
  state:{
    //如果缓存中有，则调用缓存中的token，没有则设空字符
    USERNAME: localStorage.getItem('USERNAME') ? localStorage.getItem('USERNAME'):'',
    TOKEN: localStorage.getItem('TOKEN') ? localStorage.getItem('TOKEN'):''
  },
  mutations:{
    updateToken(state,data){
      // console.log('update token',data)
      state.USERNAME=data.username
      state.TOKEN=data.token
      localStorage.setItem('USERNAME',state.USERNAME)
      localStorage.setItem('TOKEN',state.TOKEN)
    },
    deleteToken(state){
      state.USERNAME = ''
      state.TOKEN = ''
      localStorage.setItem('USERNAME', '')
      localStorage.setItem('TOKEN', '')
    }
  }
})

export default store